﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;

namespace l1cs.server.serverpackets
{
    public class S_SummonPack : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_SummonPack ).FullName );

        private const int STATUS_POISON = 1;
        private const int STATUS_INVISIBLE = 2;
        private const int STATUS_PC = 4;
        private const int STATUS_FREEZE = 8;
        private const int STATUS_BRAVE = 16;
        private const int STATUS_ELFBRAVE = 32;
        private const int STATUS_FASTMOVABLE = 64;
        private const int STATUS_GHOST = 128;

        private byte[] _byte = null;

        public S_SummonPack( L1SummonInstance pet, L1PcInstance pc )
        {
            buildPacket( pet, pc, true );
        }

        public S_SummonPack( L1SummonInstance pet, L1PcInstance pc, bool isCheckMaster )
        {
            buildPacket( pet, pc, isCheckMaster );
        }

        private void buildPacket( L1SummonInstance pet, L1PcInstance pc, bool isCheckMaster )
        {
            writeC( Opcodes.S_OPCODE_CHARPACK );
            writeH( pet.getX() );
            writeH( pet.getY() );
            writeD( pet.getId() );
            writeH( pet.getGfxId() ); // SpriteID in List.spr
            writeC( pet.getStatus() ); // Modes in List.spr
            writeC( pet.getHeading() );
            writeC( pet.getChaLightSize() ); // (Bright) - 0~15
            writeC( pet.getMoveSpeed() ); // スピード - 0:normal, 1:fast, 2:slow
            writeD( 0 );
            writeH( 0 );
            writeS( pet.getNameId() );
            writeS( pet.getTitle() );
            int status = 0;
            if ( pet.getPoison() != null )
            { // 毒状態
                if ( pet.getPoison().getEffectId() == 1 )
                {
                    status |= STATUS_POISON;
                }
            }
            writeC( status );
            writeD( 0 );
            writeS( null );
            if ( isCheckMaster && pet.isExsistMaster() )
            {
                writeS( pet.getMaster().getName() );
            }
            else
            {
                writeS( "" );
            }
            writeC( 0 ); // ??
            // HPのパーセント
            if ( pet.getMaster() != null
                    && pet.getMaster().getId() == pc.getId() )
            {
                int percent = pet.getMaxHp() != 0 ? 100 * pet.getCurrentHp()
                        / pet.getMaxHp() : 100;
                writeC( percent );
            }
            else
            {
                writeC( 0xFF );
            }
            writeC( 0 );
            writeC( pet.getLevel() ); // PC = 0, Mon = Lv
            writeC( 0 );
            writeC( 0xFF );
            writeC( 0xFF );
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = _bao.ToArray();
            }

            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_SummonPack";
        }
    }
}